package com.hnkywl.sts.service.report;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.organ.EjzhDao;
import com.hnkywl.sts.dao.organ.GsjgDao;
import com.hnkywl.sts.dao.report.EjzhmxDao;
import com.hnkywl.sts.entity.organ.Ejzh;
import com.hnkywl.sts.entity.organ.Gsjg;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: ccr
 * Date: 14-1-17
 * Time: 上午9:44
 * To change this template use File | Settings | File Templates.
 */
@Service
public class EjzhmxService {

    private EjzhmxDao ejzhmxDao;

    private GsjgDao gsjgDao;

    private EjzhDao ejzhDao;

    public String findEjzhmx(Long gsId, Long khId, Date endDate, int pageNo, int pageSize) {
        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c.add(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Map result = ejzhmxDao.findEjzhmx(gsId, khId, c.getTime(), pageNo, pageSize);
        return "{\"total\":" + result.get("total") + ",\"rows\":" + new Gson().toJson(result.get("rows")) + "}";
    }

    public String findEjzhYeMx(Long gsId, Date dateStart, Date dateEnd, int pageNo, int pageSize) {
        Calendar c = Calendar.getInstance();
        c.setTime(dateEnd);
        c.set(Calendar.HOUR, 59);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 23);
        dateEnd = c.getTime();
        c.setTime(dateStart);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Map result = ejzhmxDao.findEjzhYeMx(gsId, c.getTime(), dateEnd, pageNo, pageSize);
        return "{\"total\":" + result.get("total") + ",\"rows\":" +
                new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(result.get("rows")) + ",\"footer\":" + new Gson().toJson(result.get("footer")) + "}";
    }

    public String accountDetailsQuery(String salerCode,String parentSalerCode, Date dateStart, Date dateEnd, int pageNo, int pageSize) {
        Calendar c = Calendar.getInstance();
        c.setTime(dateEnd);
        c.set(Calendar.HOUR, 59);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 23);
        dateEnd = c.getTime();
        c.setTime(dateStart);
        c.set(Calendar.HOUR, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        Gsjg parentSaler = gsjgDao.findGsjgByCode(parentSalerCode);
        Gsjg saler = gsjgDao.findGsjgByCode(salerCode);
        Ejzh ejzh = ejzhDao.findKyyeByKhId(saler.getId(),parentSaler.getId());
        Map result = ejzhmxDao.findEjzhYeMxWjz(ejzh.getId(), c.getTime(), dateEnd, pageNo, pageSize);
        if(Double.parseDouble(result.get("total").toString())==0){
            return "{\"code\":\"0\",\"message\":\"成功\"," +
                    "\"page\":{\"totalCount\":" + 0+ ",\"pageSize\":" + pageSize + ",\"pageNo\":" + pageNo + "}," +
                    "\"result\":" +null + "}";
        }else{
            return "{\"code\":\"0\",\"message\":\"成功\"," +
                "\"page\":{\"totalCount\":" + result.get("total") + ",\"pageSize\":" + pageSize + ",\"pageNo\":" + pageNo + "}," +
                "\"result\":" + new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create().toJson(result.get("rows")) + "}";
        }
    }

    public Ejzh findEjzh(Long id) {
        return ejzhmxDao.get(id);
    }


    public Ejzh findEjzhByKh(Long khId,Long nbgsId) {
        return (Ejzh) ejzhmxDao.findUnique("from Ejzh where kh.id = ? and nbgs.id =? ", khId,nbgsId);
    }

    @Autowired
    public void setEjzhmxDao(EjzhmxDao ejzhmxDao) {
        this.ejzhmxDao = ejzhmxDao;
    }

    @Autowired
    public void setGsjgDao(GsjgDao gsjgDao) {
        this.gsjgDao = gsjgDao;
    }

    @Autowired
    public void setEjzhDao(EjzhDao ejzhDao) {
        this.ejzhDao = ejzhDao;
    }
}
